﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Moon.Sql
{
    internal class UnaryExpressionProvider
    {
        private ExpressionProvide _expression = null;

        public UnaryExpressionProvider(ExpressionProvide expression)
        {
            _expression = expression;
        }

        public string Resolve(Expression exp)
        {
            UnaryExpression ue = exp as UnaryExpression;
            string result = _expression.Resolve(ue.Operand);
            ExpressionType type = exp.NodeType;
            if (type == ExpressionType.Not)
            {
                if (result.Contains(" in "))
                {
                    result = result.Replace(" in ", " not in ");
                }
                if (result.Contains(" like "))
                {
                    result = result.Replace(" like ", " not like ");
                }
                if (result.Contains(" is null"))
                {
                    result = result.Replace(" is null ", " is not null ");
                    result = result.Replace("or", "and");
                }
                if (result.Contains("= ''"))
                {
                    result = result.Replace("= ''", "<> ''");
                    result = result.Replace("or", "and");
                }
            }
            return result;
        }
    }
}
